package main

import (
	"fmt"
	"strconv"
	"sync"
	"sync/atomic"
)

var m = sync.Map{}

// var m map[string]int

func main() {
	wg := sync.WaitGroup{}
	// m = make(map[string]int, 20000)
	for i := 0; i < 20000; i++ {
		wg.Add(1)
		go func(n int) {
			// -------------------------
			// 解决并发的问题
			key := strconv.Itoa(n)
			num := int64(n)
			atomic.AddInt64(&num, 1)
			m.Store(key, n+1)       // 赋值
			value, _ := m.Load(key) // 获取key===>value
			// -------------------------
			// 并发错误如下
			// m[strconv.Itoa(n)] = n + 1
			// fmt.Printf("k:=%v, v:=%v\n", strconv.Itoa(n), m[strconv.Itoa(n)])
			// -------------------------

			fmt.Printf("k:=%v, v:=%v\n", key, value)

			wg.Done()
		}(i)
	}
	wg.Wait()
}
